package com.res.share.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.res.share.common.Constants;
import com.res.share.model.ResTag;
import com.res.share.mapper.ResTagMapper;
import com.res.share.service.CacheService;
import com.res.share.service.IResTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-13
 */
@Service
public class ResTagServiceImpl extends ServiceImpl<ResTagMapper, ResTag> implements IResTagService {

    @Autowired
    private CacheService cacheService;

    @Override
    public List<ResTag> findTagsList() {

        List<ResTag> resTagList=new ArrayList<>();
        Object commonCache = cacheService.getCommonCache(Constants.TAGS_BASE_INFO);
        if(commonCache!=null && StringUtils.isNotEmpty(commonCache.toString())){
            resTagList=JSON.parseArray(commonCache.toString(), ResTag.class);
        }else {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("IS_DEL",0);
            queryWrapper.orderByDesc("ID");
            resTagList = baseMapper.selectList(queryWrapper);
            if(!CollectionUtils.isEmpty(resTagList)){
                cacheService.setCommonCache(Constants.TAGS_BASE_INFO,JSON.toJSONString(resTagList));
            }
        }
        return resTagList;
    }

    @Override
    public ResTag findArtTagDetail(String tagpinyin) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("TAG_PINYIN",tagpinyin);
        return baseMapper.selectOne(queryWrapper);
    }
}
